package com.neusoft.yuejian26.yuejian_26.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.neusoft.yuejian26.yuejian_26.Bean.Result;
import com.neusoft.yuejian26.yuejian_26.Bean.Userbean;
import com.neusoft.yuejian26.yuejian_26.Other.CacheInfo;
import com.neusoft.yuejian26.yuejian_26.R;
import com.neusoft.yuejian26.yuejian_26.model.UserModel;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitCallback;

import retrofit2.Call;

public class UserNameSimpActivity extends AppCompatActivity {

    public EditText intro_name;
    public Button subintro_name;
    public ImageView svg_back;
    String uid=null;
    String utel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_name_simp);
        intview();
        intEvent();
        getUserName();
    }

    private void getUserName(){
        uid=new CacheInfo(UserNameSimpActivity.this).getUserIdCache();
        if(uid!=null) {
            UserModel userModel = new UserModel();
            Call call = userModel.userIntro(uid);
            userModel.bindCallback(call, new RetrofitCallback() {
                @Override
                public void onResponse(Object o) {
                    Userbean userNamedata = (Userbean) o;
                    intro_name.setText(userNamedata.getUname());
                    utel=userNamedata.getUtel();
                }
            });
        }
    }

    private void intview(){
        intro_name=(EditText)findViewById(R.id.intro_name);
        subintro_name=(Button)findViewById(R.id.subintro_name);
        svg_back=(ImageView)findViewById(R.id.svg_back);
    }

    private void intEvent(){
        subintro_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String uname = intro_name.getText().toString().trim();
                uid=new CacheInfo(UserNameSimpActivity.this).getUserIdCache();
                if(!utel.equals("")){
                    UserModel userModel = new UserModel();
                    Call call = userModel.updateUserIntro(uid,uname,utel);
                    userModel.bindCallback(call, new RetrofitCallback() {
                        @Override
                        public void onResponse(Object o) {
                            Result updateUname = (Result) o;
                            Toast.makeText(UserNameSimpActivity.this,"更新成功",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent();
                            intent.setClass(UserNameSimpActivity.this,UserIntroActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                }
            }

        });
        svg_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}
